<?php
/**
 * Created by PhpStorm.
 * User: jksen
 * Date: 2018/1/29
 * Time: 下午5:42
 */

namespace app\common\ShaoHuoPay\Gateways\Alipay;

use app\common\ShaoHuoPay\Contracts\GatewayInterface;
use Symfony\Component\HttpFoundation\Response;

class AppGateway implements GatewayInterface
{
    /**
     * @var
     */
    protected $config;

    public function __construct($config)
    {
        $this->config = $config;
    }

    public function pay($endpoint, array $payload)
    {
        $payload['method'] = $this->getMethod();
        $payload['biz_content'] = json_encode(array_merge(
            json_decode($payload['biz_content'], true),
            ['product_code' => $this->getProductCode()]
        ));
        $payload['sign'] = AliTools::generateSign($payload, $this->config->get('private_key'));

        //Log::debug('Paying An App Order:', [$endpoint, $payload]);

        return Response::create(http_build_query($payload));
    }

    /**
     * 获取配置方法
     * @return string
     */
    protected function getMethod()
    {
        return 'alipay.trade.app.pay';
    }

    /**
     * 获取产品code码
     * @return string
     */
    protected function getProductCode()
    {
        return 'QUICK_MSECURITY_PAY';
    }
}